package com.zhxh.nzpos.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zhxh.nzpos.db.DBHelper;
import com.zhxh.nzpos.model.Card;
import com.zhxh.nzpos.utils.RandomStringUtils;

/**
 * Card DAO
 * 
 * @author Administrator
 * 
 */
public class CardDao {

	private final static String LOG_TAG = "CardDao";
	DBHelper dbHelper;

	protected static final String TBL_NAME = "card";

	public CardDao(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * add new card
	 * 
	 * @param card
	 */
	public String save(Card card) {
		Log.i(LOG_TAG, "CardDao  save " + card.toString());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		card.setId(RandomStringUtils.randomNumeric(10));
		long time = System.currentTimeMillis();
		card.setCreated(time);
		card.setModified(time);

		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"insert into card(ID,CardNumber,created,modified) values(?,?,?,?)",
					new Object[] { card.getId(), card.getCardNumber(),
							card.getCreated(), card.getModified() });

			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("CardDao  save", e.getMessage());
		}
		finally {
			db.endTransaction();// 事务结束;
			db.close();
		}

		return card.getId();

	}

	/**
	 * update a card
	 * 
	 * @param card
	 */

	public void update(Card card) {
		Log.i(LOG_TAG, "CardDao  update " + card.toString());
		card.setModified(System.currentTimeMillis());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL("update card set CardNumber=?,modified=? where ID=?",
					new Object[] { card.getCardNumber(), card.getModified(),
							card.getId() });

			// db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("CardDao  update", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		db.close();
	}

	
	/**
	 * update a card
	 * 
	 * @param card
	 */

	public int active(Card card) {
		Log.i(LOG_TAG, "CardDao  update " + card.toString());
		card.setModified(System.currentTimeMillis());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("CardNumber", card.getCardNumber());
		cv.put("modified", card.getModified());
		int count = 0;
		
		try {
			count = db.update(TBL_NAME, cv, "ID="+card.getId(),null);
			
		} catch (Exception e) {
			Log.i("CardDao  update", e.getMessage());
		}
		db.close();
		return count;
	}
	/**
	 * get a card by id
	 * 
	 * @param i
	 * @return
	 */
	public Card find(String id) {
		Log.i(LOG_TAG, "CardDao  find " + id);
		Card card = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,CardNumber,created,modified from card where ID=?",
				new String[] { id });
		if (cursor.moveToNext()) {
			card = new Card();

			card.setId(cursor.getString(cursor.getColumnIndex("ID")));
			card.setCardNumber(cursor.getString(1));
			card.setCreated(cursor.getLong(2));
			card.setModified(cursor.getLong(3));
			
		}
		cursor.close();
		db.close();
		return card;
	}

	/**
	 * delete a card by id
	 * 
	 * @param id
	 */
	public void delete(String id) {
		Log.i(LOG_TAG, "CardDao  delete card: " + id);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from card where ID=?", new Object[] { id });
		db.close();
	}

	/**
	 * delete all
	 * 
	 * @param id
	 */
	public void deleteAll() {
		Log.i(LOG_TAG, "CardDao  delete all from  card: ");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from card");
		db.close();
	}

	/**
	 * fetch cards
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<Card> getScrollData(int pageNumber, int pageSize) {
		Log.i(LOG_TAG, "CardDao  getScrollData: " + "pageNumber=" + pageNumber
				+ "  pageSize=" + pageSize);
		List<Card> cards = new ArrayList<Card>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,CardNumber,created,modified from card order by modified desc limit ?,?",
				new String[] { String.valueOf((pageNumber - 1) * pageSize),
						String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Card card = new Card();
			card.setId(cursor.getString(cursor.getColumnIndex("ID")));
			card.setCardNumber(cursor.getString(1));
			card.setCreated(cursor.getLong(2));
			card.setModified(cursor.getLong(3));
			cards.add(card);
		}
		cursor.close();
		db.close();
		return cards;
	}

	/**
	 * fetch cards
	 * 
	 * @return
	 */
	public List<Card> getAll() {
		Log.i(LOG_TAG, "CardDao  getAll: ");
		List<Card> cards = new ArrayList<Card>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,CardNumber,created,modified from card  order by modified desc", null);// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Card card = new Card();
			card.setId(cursor.getString(cursor.getColumnIndex("ID")));
			card.setCardNumber(cursor.getString(1));
			card.setCreated(cursor.getLong(2));
			card.setModified(cursor.getLong(3));
			cards.add(card);
		}
		cursor.close();
		db.close();
		return cards;
	}

	/**
	 * get card's count
	 * 
	 * @return
	 */
	public long getCount() {
		Log.i(LOG_TAG, "CardDao  getCount: ");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TBL_NAME);
		db.close();
		return count;
	}

}
